var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/note',{
    useUnifiedTopology: true,
    useNewUrlParser: true
});

var Schema = mongoose.Schema;

var userSchema = new Schema({
    username: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    gender: Number,
    avatar: {
        type: String,
        default: '/public/img/default.jpeg'
    }
})




var User = mongoose.model('User', userSchema)

// var admin = new User({
//     username:'MOZZ',
//     email: '770982290@qq.com',
//     password:'123',
//     gender:1,
//     avatar:'/public/img/defalut.jpeg'
// }).save(function (err, ret){
//     if(err){
//         console.log('保存失败')
//         console.log(err)
//     }else{
//         console.log('保存成功')
//         console.log(ret)
//     }
// })

// 封装查询一个的方法为 promise 对象
User.findOnePromise = function(obj){
    return new Promise(function(resolve, reject){
        User.findOne(obj,function(err, ret){
            if(err){
                reject(err)
            }else{
                resolve(ret)
            }
        })
    })
} 

module.exports = User